<?php
namespace TennisTournament\Model;

use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;

/**
 * @ORM\Entity
 * @ORM\Table(name="`Match`")
 */
class Match {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var integer
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Player")
     * @ORM\JoinColumn(name="player_host_id", referencedColumnName="id")
     * @var Player
     */
    protected $playerHost;

    /**
     * @ORM\ManyToOne(targetEntity="Player")
     * @ORM\JoinColumn(name="player_guest_id", referencedColumnName="id")
     * @var Player
     */
    protected $playerGuest;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $winner;


    /**
     * @ORM\Column(name="stage_of_tournament", type="string")
     * @var string
     */
    protected $stageOfTournament;

    /**
     * @ORM\OneToMany(targetEntity="Result", mappedBy="match", cascade="persist")
     * @var Result
     */
    protected $results;

    /**
     * @ORM\Column(type="date", name="`date`")
     * @var date
     */
    protected $date;

    public function __construct(Player $host, Player $guest) {
        $this->playerHost = $host;
        $this->playerGuest = $guest;
    }

    /**
     * @param int $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param \TennisTournament\Model\Player $playerGuest
     */
    public function setPlayerGuest($playerGuest) {
        $this->playerGuest = $playerGuest;
    }

    /**
     * @return \TennisTournament\Model\Player
     */
    public function getPlayerGuest() {
        return $this->playerGuest;
    }

    /**
     * @param \TennisTournament\Model\Player $playerHost
     */
    public function setPlayerHost($playerHost) {
        $this->playerHost = $playerHost;
    }

    /**
     * @return \TennisTournament\Model\Player
     */
    public function getPlayerHost() {
        return $this->playerHost;
    }


    /**
     * @param string $stageOfTournament
     * @throws InvalidArgumentException if $winner is not compatible value.
     */
    public function setStageOfTournament($stageOfTournament) {
        if (!in_array($stageOfTournament, array(StageOfTournamentEnum::FINAL_STAGE,
                StageOfTournamentEnum::GROUP_STAGE, StageOfTournamentEnum::QUARTERFINAL,
                StageOfTournamentEnum::SECOND_STAGE, StageOfTournamentEnum::SEMIFINAL,
                StageOfTournamentEnum::THIRD_PLACE))) {
            throw new InvalidArgumentException("Invalid winner type");
        }
        $this->stageOfTournament = $stageOfTournament;
    }

    /**
     * @return string
     */
    public function getStageOfTournament() {
        return $this->stageOfTournament;
    }

    /**
     * @param string $winner
     * @throws InvalidArgumentException if $winner is not compatible value.
     */
    public function setWinner($winner) {
        if (!in_array($winner, array(WinnerEnum::GUEST, WinnerEnum::HOST))) {
            throw new InvalidArgumentException("Invalid winner type");
        }
        $this->winner = $winner;
    }

    /**
     * @return string
     */
    public function getWinner() {
        return $this->winner;
    }

    /**
     * @param $results array
     */
    public function setResults($results) {
        $this->results = $results;
    }

    /**
     * @return array
     */
    public function getResults() {
        return $this->results;
    }

    /**
     */
    public function setDate($date) {
        $this->date = $date;
    }

    /**
     * @return date
     */
    public function getDate() {
        return $this->date;
    }

    /**
     * Convert a Match into an associative array
     * @return array
     */
    public function toArray() {
        $resultsCollection = array();
        /** @var $result Result */
        foreach ($this->results as $result) {
            array_push($resultsCollection, $result->toArray());
        }

        return array(
            'id'                =>  $this->id,
            'playerHost'        =>  $this->playerHost->toArray(),
            'playerGuest'       =>  $this->playerGuest->toArray(),
            'results'           =>  count($resultsCollection) < 1 ? null : $resultsCollection,
            'stageOfTournament' =>  $this->stageOfTournament,
            'winner'            =>  $this->winner,
            'date'              =>  $this->date != null ? $this->date->format('Y-m-d') : null
        );
    }
}